import { message } from 'antd';
import {
  queryRole,
  queryRoleType,
  queryRoleName,
  queryUserType,
  queryUserOrg,
  addRole,
  rmRole,
  roleExport,
  editRole,
} from '../services/api';

export default {
  namespace: 'role',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    roleType: [],
    roleName: [],
    userType: [],
    userOrg: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryRole, payload);
      //console.log('role', response);
      if (response ?.amRoleManage)
        yield put({
          type: 'save',
          payload: response.amRoleManage,
        });
    },
    *fetchType({ payload }, { call, put }) {
      const response = yield call(queryRoleType);
      //console.log('roleType', response);
      if (response ?.roleTypeCodes)
        yield put({
          type: 'saveType',
          payload: response.roleTypeCodes,
        });
    },
    *fetchUserType({ payload }, { call, put }) {
      const response = yield call(queryUserType);
      //console.log('roleType', response);
      if (response ?.userTypeCodes)
        yield put({
          type: 'saveUserType',
          payload: response.userTypeCodes,
        });
    },
    *fetchUserOrg({ payload }, { call, put }) {
      const response = yield call(queryUserOrg);
      //console.log('roleType', response);
      if (response ?.userOrgNameCodes)
        yield put({
          type: 'saveUserOrg',
          payload: response.userOrgNameCodes,
        });
    },

    *fetchName({ payload }, { call, put }) {
      const response = yield call(queryRoleName);
      //console.log('roleName', response);
      if (response ?.roleNameCodes)
        yield put({
          type: 'saveName',
          payload: response.roleNameCodes,
        });
    },
    *add({ payload, callback }, { call }) {
      const response = yield call(addRole, payload);
      //console.log('add result', response);
      if (response ?.result ?.code === 2001) {
        message.error('提交出错，请检查');
      } else if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      const { roleId } = payload;
      const response = yield call(rmRole, payload);
      //console.log('add result', response);
      if (response ?.result ?.code === 2001) {
        message.error('删除失败，请检查');
      } else {
        yield put({
          type: 'removeRole',
          payload: { roleId },
        });
        if (callback) callback();
      }
    },
    *export({ payload, callback }, { call }) {
      const response = yield call(roleExport, payload);
      //console.log('add result', response);
      if (response ?.result ?.code === 2001) {
        message.error('提交出错，请检查');
      } else if (callback) callback(response.expAmRoleManage.expFileUrl);
    },
    *edit({ payload, callback }, { call }) {
      const response = yield call(editRole, payload);
      //console.log('add result', response);
      if (response ?.result ?.code === 2001) {
        message.error('提交出错，请检查');
      } else if (callback) {
        callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: {
          list: action.payload,
        },
      };
    },
    saveType(state, action) {
      return {
        ...state,
        roleType: action.payload,
      };
    },
    saveUserType(state, action) {
      return {
        ...state,
        userType: action.payload,
      };
    },
    saveUserOrg(state, action) {
      return {
        ...state,
        userOrg: action.payload,
      };
    },
    saveName(state, action) {
      return {
        ...state,
        roleName: action.payload,
      };
    },
    removeRole(state, action) {
      const newlist = state.data.list.filter(
        item => action.payload.roleId.indexOf(item.roleId) === -1
      );
      return {
        ...state,
        data: { list: newlist, pagination: state.data.pagination },
      };
    },
  },
};
